package com.qwt.part_time_api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.qwt.part_time_api.error.BusinessException;
import com.qwt.part_time_api.error.ErrorCode;
import com.qwt.part_time_api.mapper.AreaMapper;
import com.qwt.part_time_api.pojo.Area;
import com.qwt.part_time_api.service.AreaService;
import com.qwt.part_time_api.service.RegionService;
import com.qwt.part_time_api.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 君子慎独
 * @create 2022/1/20 0020 17:25
 */
@Service
@Slf4j
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private RegionService regionService;

    @Override
    public Result findNameList(String name) throws BusinessException {
        log.info("获取所有区级");
        if (name == null) {
            log.info("城市为空");
            throw new BusinessException(ErrorCode.ERR_NO_NULL);
        }
        //查询出市级编码
        String regionCode = regionService.findRegionCodeByName(name);

        if (StringUtils.isEmpty(regionCode)) {
            log.info("无该城市");
            throw new BusinessException(ErrorCode.ERR_NO_SUCH_CITY);
        }
        LambdaQueryWrapper<Area> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Area::getRegionCode, regionCode);
        List<Area> areas = areaMapper.selectList(wrapper);

        return Result.success(areas);
    }
}
